package com.mark.crawler;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.summba.common.Common;

public class StockCrawler {
	private List<String> stockCoreList;
	private int iWaitTime = 500;
	
	/*
	 * ichart.finance.yahoo.com/table.csv?s=601398.ss
	 * 
	 */
	public StockCrawler(List<String> StockList) throws Exception{
		this.stockCoreList = StockList;
		this.runFetch();
	}
	
	private void runFetch() throws Exception{
		for (Iterator<String> iterator = stockCoreList.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			String currentStockCore = java.net.URLEncoder.encode( string, "utf-8");
			String currentGetUrl = "http://ichart.finance.yahoo.com/table.csv?s=" + currentStockCore ;
			System.out.println("URL "+currentGetUrl);
			URL url = new URL(currentGetUrl);
			URLConnection conn = url.openConnection();
			BufferedReader br = new BufferedReader( new InputStreamReader( 
					conn.getInputStream(),"utf-8" ) );
			System.out.println("File is "+System.getProperty("user.dir")+
					"//data//stock"+currentStockCore+".sdb");
			Common.writeFile(System.getProperty("user.dir")+"//data//stock//"+currentStockCore+".sdb", br);	
			Thread.sleep(iWaitTime);
		}
	}

}
